Public Module Functions

#Region " Constants "
    Public Const SRCCOPY As Int32 = &HCC0020
    Public Const PS_SOLID As Int32 = 0
#End Region

#Region " Types "
    Public Structure RECT
        Public Left As Integer
        Public Top As Integer
        Public Right As Integer
        Public Bottom As Integer
    End Structure

    Public Structure POINTAPI
        Public x As Integer
        Public y As Integer
    End Structure

    Public Structure SIZE
        Public x As Integer
        Public y As Integer
    End Structure
#End Region

#Region " Functions "
#Region " GDI "
    '#Region " Objects "
    Public Declare Function SelectObject Lib "gdi32" (ByVal hDC As Int32, ByVal hObject As Int32) As Int32
    Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Int32) As Int32
    Public Declare Function GetStockObject Lib "gdi32" (ByVal hObject As Int32) As Int32
    '#End Region

    '#Region " DC "
    Public Declare Function GetDesktopWindow Lib "user32" () As Int32
    Public Declare Function GetDC Lib "user32" (ByVal hWnd As Int32) As Int32
    Public Declare Function GetWindowDC Lib "user32" (ByVal hWnd As Int32) As Int32
    Public Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDC As Int32) As Int32
    Public Declare Function DeleteDC Lib "gdi32" (ByVal hDC As Int32) As Int32
    Public Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Int32, ByVal hDC As Int32) As Int32
    '#End Region

    '#Region " Pens "
    Public Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As Integer, ByVal nWidth As Integer, ByVal crColor As Integer) As Int32
    '#End Region

    '#Region " Brushes "
    Public Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Integer) As Int32
    '#End Region

    '#Region " Background mode "
    Public Declare Function SetROP2 Lib "gdi32" (ByVal hDC As Integer, ByVal nDrawMode As Integer) As Integer
    Public Declare Function SetBkColor Lib "gdi32" (ByVal hDC As Integer, ByVal crColor As Integer) As Integer
    Public Declare Function SetBkMode Lib "gdi32" (ByVal hDC As Integer, ByVal nBkMode As Integer) As Integer
    '#End Region

    '#Region " Bitmaps "
    Public Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Int32, ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hSrcDC As Int32, ByVal xSrc As Integer, ByVal ySrc As Integer, ByVal dwRop As Integer) As Integer
    Public Declare Function StretchBlt Lib "gdi32" (ByVal hDC As Int32, ByVal X As Int32, ByVal Y As Int32, ByVal nWidth As Int32, ByVal nHeight As Int32, ByVal hSrcDC As Int32, ByVal xSrc As Int32, ByVal ySrc As Int32, ByVal nSrcWidth As Int32, ByVal nSrcHeight As Int32, ByVal dwRop As Int32) As Int32
    Public Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hDC As Int32, ByVal nWidth As Integer, ByVal nHeight As Integer) As Int32
    '#End Region

    '#Region " Filled shapes "
    Public Declare Function FillRect Lib "user32" (ByVal hDC As Int32, ByRef lpRect As RECT, ByVal hBrush As Int32) As Integer
    Public Declare Function Polygon Lib "gdi32" (ByVal hDC As Int32, ByRef lpPoint As POINTAPI, ByVal nCount As Int32) As Int32
    '#End Region

    '#Region " Lines and curves "
    Public Declare Function SetPixelV Lib "gdi32" (ByVal hDC As Int32, ByVal X As Int32, ByVal Y As Int32, ByVal crColor As Int32) As Int32
    Public Declare Function LineTo Lib "gdi32" (ByVal hDC As Int32, ByVal X As Integer, ByVal Y As Integer) As Integer
    Public Declare Function MoveToEx Lib "gdi32" (ByVal hDC As Int32, ByVal X As Integer, ByVal Y As Integer, ByRef lpPoint As POINTAPI) As Integer
    Public Declare Function Ellipse Lib "gdi32" (ByVal hDC As Int32, ByVal X1 As Integer, ByVal Y1 As Integer, ByVal X2 As Integer, ByVal Y2 As Integer) As Integer
    Public Declare Function Rectangle Lib "gdi32" (ByVal hDC As Int32, ByVal X1 As Integer, ByVal Y1 As Integer, ByVal X2 As Integer, ByVal Y2 As Integer) As Integer
    Public Declare Function RoundRect Lib "gdi32" (ByVal hDC As Int32, ByVal X1 As Int32, ByVal Y1 As Int32, ByVal X2 As Int32, ByVal Y2 As Int32, ByVal X3 As Int32, ByVal Y3 As Int32) As Int32
    Public Declare Function DrawFocusRect Lib "user32" (ByVal hDC As Int32, ByRef lpRect As RECT) As Int32
    '#End Region

    '#Region " Text "
    Public Declare Auto Function CreateFont Lib "gdi32" ( _
            ByVal nHeight As Int32, _
            ByVal nWidth As Int32, _
            ByVal nEscapement As Int32, _
            ByVal nOrientation As Int32, _
            ByVal fnWeight As Int32, _
            ByVal fdwItalic As Int32, _
            ByVal fdwUnderline As Int32, _
            ByVal fdwStrikeOut As Int32, _
            ByVal fdwCharSet As Int32, _
            ByVal fdwOutputPrecision As Int32, _
            ByVal fdwClipPrecision As Int32, _
            ByVal fdwQuality As Int32, _
            ByVal fdwPitchAndFamily As Int32, _
            ByVal lpszFace As System.String) As Int32

    Public Declare Function TextOut Lib "gdi32" (ByVal hDC As Int32, ByVal X As Int32, ByVal Y As Int32, ByVal lpString As String, ByVal nCount As Int32) As Int32
    Public Declare Function ExtTextOut Lib "gdi32" (ByVal hDC As Int32, ByVal X As Int32, ByVal Y As Int32, ByVal fuOptions As Int32, ByRef rect As RECT, ByVal lpString As String, ByVal cbCount As Int32, ByVal stringpDx As Int32) As System.Boolean
    Public Declare Function DrawText Lib "user32" (ByVal hDC As Int32, ByVal lpStr As String, ByVal nCount As Int32, ByRef lpRect As RECT, ByVal wFormat As Int32) As Int32
    Public Declare Function SetTextColor Lib "gdi32" (ByVal hDC As Int32, ByVal crColor As Int32) As Int32
    Public Declare Function GetTextExtentPoint32 Lib "gdi32" (ByVal hDC As Int32, ByVal lpsz As String, ByVal cbString As Int32, ByRef lpSize As SIZE) As Int32

    '#End Region

#Region "Misc"
    Public Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Integer, ByVal nIndex As Integer) As Integer
    Public Declare Function MulDiv Lib "kernel32" (ByVal nNumber As Integer, ByVal nNumerator As Integer, ByVal nDenominator As Integer) As Integer
#End Region

#End Region

#Region " Window functions "
    Public Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Int32) As Integer
#End Region

#Region " Time "
    Public Declare Function timeGetTime Lib "WinMM.dll" () As Integer
    Public Declare Function QueryPerformanceCounter Lib "kernel32" (ByRef PC As Int64) As Integer
    Public Declare Function QueryPerformanceFrequency Lib "kernel32" (ByRef PC As Int64) As Integer
#End Region
#End Region

#Region " Macros "

#Region " GDI "
    Public Function RGB(ByVal R As Integer, ByVal G As Integer, ByVal B As Integer) As Integer
        RGB = R Or (G * 256) Or (B * 65536)
    End Function

    Public Function ToRECT(ByVal r As System.Drawing.Rectangle) As RECT
        With ToRECT
            .Left = r.Left
            .Top = r.Top
            .Right = r.Right
            .Bottom = r.Bottom
        End With
    End Function

    Public Sub FillRectangle(ByVal DC As Int32, ByVal Col As System.Drawing.Color, ByVal r As System.Drawing.Rectangle)
        Dim hBrush As Int32 = CreateSolidBrush(System.Drawing.ColorTranslator.ToWin32(Col))
        FillRect(DC, ToRECT(r), hBrush)
        DeleteObject(hBrush)
    End Sub
#End Region

#Region " Time "
    Public Function Ticks() As Int64
        ' TODO: SOMETIME: replace with timeGetTime on Win98...
        Dim t As Int64
        QueryPerformanceCounter(t)
        Return t
    End Function

    Public Function PerformanceCounterFrequency() As Int64
        Dim t As Int64
        QueryPerformanceFrequency(t)
        Return t
    End Function
#End Region

#Region " VB "

    Public Sub Swap(ByRef x1 As Double, ByRef x2 As Double)
        Dim t As Double = x1
        x1 = x2
        x2 = t
    End Sub

    Public Sub Order(ByRef x1 As Double, ByRef x2 As Double)
        If x1 > x2 Then
            Dim t As Double = x1
            x1 = x2
            x2 = t
        End If
    End Sub

#End Region

#End Region
End Module